package com.bk.controller;

import com.bk.interceptor.BKInterceptor;
import com.cjw.model.DBMap;
import com.jfinal.aop.Before;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.moomi.common.model.BkUser;
import com.moomi.common.model.BkUserShop;
import com.utils.AuthUtils;
import com.utils.QNUtils;

@Before({ BKInterceptor.class })
public class BKController extends BKNormalController {
	// BKAuthUtils authUtils = new BKAuthUtils(this);

	protected int defaultShopId = 23;
	protected final int UPLOAD_MODE = PropKit.getInt("UPLOAD_MODE", 2);

	protected BkUser getUser() {
		String uid = AuthUtils.getTokenUid(this);
		BkUser user = BkUser.dao.findById(Integer.parseInt(uid));
		return user;
	}

	protected String getPrefix() {
		return "CJWBooking";
	}

	protected Integer getUserId() {
		// return authUtils.getUserId();
		String uid = AuthUtils.getTokenUid(this);
		if (uid != null) {
			// BkUser user = (BkUser) authUtils.getToken().getInfo();
			BkUser user = BkUser.dao.findById(Integer.parseInt(uid));
			return user.getId();
		}
		return null;
	}

	public Integer getShopId() {
		try {
			BkUser user = getUser();
			Integer shopId = getSessionAttr("shopId");
			if (shopId == null) {
				// shopId = getUser()
				if (user != null) {
					shopId = user.getSelectedShop();
				}
			}
			if (shopId == null) {
				Integer userId = getUserId();
				if (userId == null) {
					return null;
				}
				DBMap userShopMap = new DBMap();
				userShopMap.put("userId", user.getId());
				BkUserShop userShop = BkUserShop.dao.searchFirst(userShopMap);
				shopId = userShop.getShopId();
				// shopId = authUtils.getFirshShopId();
				user.setSelectedShop(shopId);
				user.update();
				return shopId;
			} else {
				return shopId;
			}
		} catch (Exception e) {
			LogKit.error("get shop id error", e);
			return defaultShopId;
		}
		// System.out.println("user default shop id -> " + defaultShopId);
		// return defaultShopId;
	}

	protected void setShopId(int shopId) {
		setSessionAttr("shopId", shopId);
	}

	public Integer getItemId() {
		return getParaToInt("itemId");
	}

	public Integer getSelectionId() {
		return getParaToInt("selectionId");
	}

	@Override
	public void update() {
		renderText("update");
	}

	@Override
	public void index() {
		setAttr("deleteUrl", "delete");
		setAttr("infoUrl", "info");
		renderText("index");
	}

	@Override
	public void delete() {
		renderText("delete");
	}

	@Override
	public void info() {
		setAttr("action", "update");
		render("admin-form.html");
	}

	public String uploadImage(java.io.File file) {
		// System.out.println("rewrite");
		// String qnhost = "http://img.cenjiawen.com";
		// qnhost = "http://obyg8ilnz.bkt.clouddn.com";

		// QNUtils utils = new
		// QNUtils("JrRlQ9yhAxFonToRC_hOfqJ8VBvy786T2TQGn7D8",
		// "rXztWGzgrRilAyJysgdvxJFgPs63DGTZta1g6YcI", "cjwdevelop",
		// "http://img.cenjiawen.com", true);
		//
		// UUID uuid = UUID.randomUUID();
		// String key = uuid.toString();
		// String url = utils.upload(file, key);
		//
		// String imageHost = "http://app.cenjiawen.com/bk/image/?key=" + url;
		// // String imageHost = "http://localhost:9090/bk/image/?key=" + url;
		// System.out.println(imageHost);
		// return imageHost;
		// File imageFile = getFileParam("image");
		QNUtils qnUtils = QNUtils.dgUtils();
		qnUtils.project = "bk";
		qnUtils.simpleStyle = "";
		String url = qnUtils.upload(file);
		// System.out.println("url: " + url);F
		return url;
	}

	public void image() {
		String key = getPara("key");
		QNUtils utils = new QNUtils("JrRlQ9yhAxFonToRC_hOfqJ8VBvy786T2TQGn7D8",
				"rXztWGzgrRilAyJysgdvxJFgPs63DGTZta1g6YcI", "cjwdevelop", "http://img.cenjiawen.com", true);
		String url = utils.download(key);
		renderText(url);
		redirect(url);
	}
}
